package com.rahms.server.controller;

import com.rahms.common.result.R;
import com.rahms.server.service.ConstantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 常量管理控制器
 */
@RestController
@RequestMapping("/constants")
public class ConstantController {

    @Autowired
    private ConstantService constantService;

    /**
     * 获取所有消息常量
     */
    @GetMapping("/messages")
    public R<Map<String, String>> getAllMessageConstants() {
        Map<String, String> messages = constantService.getAllMessageConstants();
        return R.success(messages).add("message", "消息常量获取成功");
    }

    /**
     * 获取所有缓存常量
     */
    @GetMapping("/cache")
    public R<Map<String, Object>> getAllCacheConstants() {
        Map<String, Object> cacheConstants = constantService.getAllCacheConstants();
        return R.success(cacheConstants).add("message", "缓存常量获取成功");
    }

    /**
     * 获取所有JWT声明常量
     */
    @GetMapping("/jwt-claims")
    public R<Map<String, String>> getAllJwtClaimsConstants() {
        Map<String, String> jwtClaims = constantService.getAllJwtClaimsConstants();
        return R.success(jwtClaims).add("message", "JWT声明常量获取成功");
    }

    /**
     * 获取所有自动填充常量
     */
    @GetMapping("/auto-fill")
    public R<Map<String, String>> getAllAutoFillConstants() {
        Map<String, String> autoFill = constantService.getAllAutoFillConstants();
        return R.success(autoFill).add("message", "自动填充常量获取成功");
    }

    /**
     * 获取所有常量
     */
    @GetMapping("/all")
    public R<Map<String, Object>> getAllConstants() {
        Map<String, Object> allConstants = constantService.getAllConstants();
        return R.success(allConstants).add("message", "所有常量获取成功");
    }

    /**
     * 根据类型获取常量
     */
    @GetMapping("/by-type")
    public R<Object> getConstantsByType(@RequestParam String type) {
        switch (type.toLowerCase()) {
            case "messages":
                return R.success(constantService.getAllMessageConstants());
            case "cache":
                return R.success(constantService.getAllCacheConstants());
            case "jwt-claims":
                return R.success(constantService.getAllJwtClaimsConstants());
            case "auto-fill":
                return R.success(constantService.getAllAutoFillConstants());
            case "all":
                return R.success(constantService.getAllConstants());
            default:
                return R.error("不支持的常量类型: " + type);
        }
    }
}
